package com.yanqu.road.dao.impl.activity.hell;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.hell.HellAgeConfig;
import com.yanqu.road.entity.activity.hell.HellEventConfig;
import com.yanqu.road.entity.activity.hell.HellNpcConfig;
import com.yanqu.road.entity.activity.hell.HellQualityConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HellConfigDaoImpl extends TempDao {

    public static String getHellNpcCommentSql = "select * from t_s_activity_hell_npc where activityId= ?";
    public Map<Integer, HellNpcConfig> getHellNpcMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, HellNpcConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getHellNpcCommentSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HellNpcConfig data = new HellNpcConfig();
                data.setNpcId(rs.getInt("npcId"));
                data.setName(rs.getString("name"));
                data.setWeight(rs.getInt("weight"));
                data.setIcon(rs.getInt("icon"));
                result.put(data.getNpcId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String getHellQualitySql = "select * from t_s_activity_hell_quality where activityId= ?";
    public Map<Integer, HellQualityConfig> getHellQualityMap(int activityId){
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, HellQualityConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getHellQualitySql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HellQualityConfig data = new HellQualityConfig();
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setPlaceName(rs.getString("placeName"));
                data.setPlaceDesc(rs.getString("placeDesc"));
                data.setIcon(rs.getString("icon"));
                data.setRewards(rs.getInt("rewards"));
                data.setWeight(rs.getInt("weight"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }


    public static String getHellEventSql = "select * from t_s_activity_hell_event where activityId= ?";
    //qualityId eventType id
    public Map<Integer, Map<Integer,Map<Integer, HellEventConfig>>> getHellEventMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer,Map<Integer, HellEventConfig>>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getHellEventSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                int eventType = rs.getInt("eventType");
                List<Integer> qualityIdList = StringUtils.stringToIntegerList(rs.getString("qualityId"),",");
                for(int qualityId : qualityIdList){
                    HellEventConfig data = new HellEventConfig();
                    data.setId(id);
                    data.setEventType(eventType);
                    data.setQualityId(qualityId);
                    if(!result.containsKey(qualityId)){
                        result.put(qualityId,new ConcurrentHashMap<>());
                    }
                    if(!result.get(qualityId).containsKey(eventType)){
                        result.get(qualityId).put(eventType,new ConcurrentHashMap<>());
                    }
                    result.get(qualityId).get(eventType).put(data.getId(),data);
                }
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String getHellAgeSql = "select * from t_s_activity_hell_age where activityId= ?";
    public Map<Integer, HellAgeConfig> getHellAgeMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, HellAgeConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getHellAgeSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HellAgeConfig data = new HellAgeConfig();
                data.setId(rs.getInt("id"));
                data.setAge(rs.getString("age"));
                data.setEventNum(rs.getString("eventNum"));
                List<Integer> eventList = StringUtils.stringToIntegerList(rs.getString("eventNum"),",");
                Map<Integer,Integer> eventMap = new ConcurrentHashMap<>();
                eventMap.put(1,eventList.get(0));
                eventMap.put(2,eventList.get(1));
                eventMap.put(3,eventList.get(2));
                eventMap.put(4,eventList.get(3));
                data.setNeedEventMap(eventMap);
                data.setWeight(rs.getInt("weight"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }
}
